package com.fishery.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fishery.base.entity.Catch;
import com.fishery.base.entity.GermchitManagement;
import com.fishery.base.entity.GermchitPond;
import com.fishery.base.entity.vo.FarmingVo;
import com.fishery.base.mapper.CatchMapper;
import com.fishery.base.mapper.GermchitManagementMapper;
import com.fishery.base.mapper.GermchitPondMapper;
import com.fishery.base.service.GermchitPondService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.ExecutorService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ghost
 * @since 2021-01-15
 */
@Service
public class GermchitPondServiceImpl extends ServiceImpl<GermchitPondMapper, GermchitPond> implements GermchitPondService {

	@Autowired
	private GermchitPondMapper germchitPondMapper;

	@Autowired
	private GermchitManagementMapper germchitManagementMapper;

	@Autowired
	private CatchMapper catchMapper;

	@Autowired
	private ExecutorService threadPool;

	/**
	 * 通过池塘ID和种苗ID向池塘投苗
	 */
	@Override
	@Transactional
	public boolean farming(FarmingVo farmingVo) {
		// 查询该池塘是否处于养殖状态
		QueryWrapper pondWrapper = new QueryWrapper();
		pondWrapper.eq("pond_id", farmingVo.getPondId());
		Integer line = germchitPondMapper.selectCount(pondWrapper);
		if (line > 0) {
			// 池塘已经处于养殖状态
			return false;
		}
		// 获取该类种苗在基地的库存信息
		QueryWrapper germchitWrapper = new QueryWrapper();
		germchitWrapper.eq("germchit_id", farmingVo.getGermchitId());
		germchitWrapper.eq("base_id", farmingVo.getBaseId());
		GermchitManagement germchitManagement = germchitManagementMapper.selectOne(germchitWrapper);
		// 判断库存中是否有足够的该种苗
		if (germchitManagement.getGermchitSurplusNumber() < farmingVo.getInputNum()) {
			// 不足
			return false;
		}
		// 足够，减少库存的对应的种苗数量
		threadPool.submit(() ->
				germchitManagementMapper.reduceGermchitAmount(farmingVo.getBaseId(), farmingVo.getGermchitId(), farmingVo.getInputNum()));
		// 添加池塘养殖记录，进行养殖
		GermchitPond germchitPond = new GermchitPond();
		germchitPond.setInputNum(farmingVo.getInputNum());
		germchitPond.setGermchitId(farmingVo.getGermchitId());
		germchitPond.setPondId(farmingVo.getPondId());
		germchitPond.setSeedingTime(new Date());
		germchitPond.setSurplusWeight(farmingVo.getInputWeight());
		germchitPond.setOutputWeight(germchitPond.getSurplusWeight());
		int ret = germchitPondMapper.insert(germchitPond);
		return ret > 0;
	}

	@Override
	@Transactional
	public boolean catching(Catch catchVo) {
		// 获取池塘的剩余量信息
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("pond_id", catchVo.getPondId());
		GermchitPond germchitPond = germchitPondMapper.selectOne(wrapper);
		// 判断是否足够此次的捕捞期望
		if (germchitPond.getSurplusWeight() < catchVo.getTotalWeight()) {
			// 剩余量不够此次捕捞
			return false;
		}
		// 足够捕捞，进行捕捞
		// 减少池塘的剩余量
		germchitPond.setSurplusWeight(germchitPond.getSurplusWeight() - catchVo.getTotalWeight());
		// 判断是否池塘已经全部捕捞完了
		if (germchitPond.getSurplusWeight() < 0.0001) {
			// 已经全部捕捞完了，直接清空池塘，清空喂养信息
			threadPool.submit(() -> {
				// 清空池塘
				germchitPondMapper.deleteById(germchitPond.getId());
			});
		} else {
			// 未全部捕捞完，需要更新池塘捕捞信息
			threadPool.submit(() -> {
				// 更新上次捕捞时间
				germchitPond.setCatchTime(new Date());
				// 更新捕捞状态
				germchitPond.setCatchStatus(1);
				// 更新池塘的目前的养殖情况
				germchitPondMapper.updateById(germchitPond);
			});
		}
		// 存放捕捞记录
		catchVo.setSurplusAmount(catchVo.getCatchAmount());
		catchVo.setSurplusWeight(catchVo.getTotalWeight());
		catchMapper.insert(catchVo);
		return true;
	}

	@Override
	public Date traceSeedingTimeById(String id) {
		return germchitPondMapper.findSeedingTimeByIdEvenDelete(id);
	}
}
